//
//  MusicViewController.m
//  Feel_at_home
//
//  Created by 王静雨 on 15/9/29.
//  Copyright © 2015年 Feel_at_home. All rights reserved.
//

#import "MusicViewController.h"
#import "Constants.h"
#import <AVFoundation/AVFoundation.h>
#import "SingleViewController.h"
#import "WidthAndHeight.pch"
#import <MJRefresh.h>
#import "JokeViewController.h"
#import "WidthAndHeight.pch"

@interface MusicViewController ()

@property (nonatomic ,strong) UIButton *playButton;
@property (nonatomic ,strong) UIImageView *platImage;
@property (nonatomic ,assign) BOOL y;
@property (nonatomic ,strong) AVPlayer *player;
@property (nonatomic ,strong) AVPlayerItem *playerItem;
@property (nonatomic ,strong) UIImageView *imageView1;
@property (nonatomic ,strong) UIImageView *imageView;
@property (nonatomic ,strong) NSTimer *timer;
@property (nonatomic ,strong) UIButton *buttonback;

@end

@implementation MusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"kaixin"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButton:)];
    
    self.title = kHappy;
    
    // Do any additional setup after loading the view.
    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cat"]];
    self.imageView.frame = CGRectMake(0, 0, VCWIDTH, VCHEIGH);
    self.imageView.backgroundColor = [UIColor colorWithRed:1.000 green:0.438 blue:0.502 alpha:1.000];
    self.imageView.alpha = 1;
    UIVisualEffectView *visya = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    visya.frame = [UIScreen mainScreen].bounds;
    visya.alpha = 0.7;
    [self.imageView addSubview:visya];
    [self.view addSubview:self.imageView];
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playButton.frame =CGRectMake(160*WIDTH, 450* HEIGH, 50*WIDTH, 50* HEIGH);
    [self.view addSubview:self.playButton];
    
    self.imageView1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cat"]];
    self.imageView1.frame = CGRectMake(50* WIDTH, 100* HEIGH, 280* WIDTH, 280* WIDTH);
    [self.view addSubview:self.imageView1];
    
    self.imageView1.layer.cornerRadius = 140* WIDTH ;
    
    self.imageView1.layer.backgroundColor = [UIColor redColor].CGColor;
    self.imageView1.layer.borderWidth = 1;
   
    self.imageView1.layer.masksToBounds = YES;
    [self.imageView1 setBackgroundColor:[UIColor greenColor]];
    self.imageView1.tag = 10;
    
    
    if (self.y == NO ) {
        self.player = [[AVPlayer alloc] init];
        NSURL *url = [NSURL URLWithString:@HappyMusicUrl];
        self.playerItem = [[AVPlayerItem alloc] initWithURL:url];
        [self.player replaceCurrentItemWithPlayerItem:self.playerItem];
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-iconfontbofang"] forState:UIControlStateNormal];
        
    }else
    {
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-iconfontzanting"] forState:UIControlStateNormal];
    }
    [self.playButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)rightButton:(UIButton *)button
{
    JokeViewController *joke = [[JokeViewController alloc] init];
    [self.navigationController pushViewController:joke animated:YES];
}

- (void)buttonClick:(UIButton *)button
{
    if (self.y == NO  || self.timer == nil) {
        [self.player play];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(xuanzhuang) userInfo:nil repeats:YES];
        
        self.y = YES;
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-iconfontzanting"] forState:UIControlStateNormal];
    }else
    {
        [self.player pause];
        self.y = NO;
        [self.timer invalidate];
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-iconfontbofang"] forState:UIControlStateNormal];
    }
}

- (void)xuanzhuang
{
    UIImageView *image1 = (UIImageView *)[self.view viewWithTag:10];
    image1.transform = CGAffineTransformRotate(image1.transform, -M_PI_4 * 1 /50);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
